
	var _ws;
	var _socket_url="wss://socket.jinyunweb.com";
	var _message=function(data,type){
		var message={
			type:type,
			data:data
		};
		message=JSON.stringify(message);
		//console.log('发送消息：');console.log(message);
		_ws.send({'data':message});
	}
	var jinyun_socket={};
	jinyun_socket.send2uid=function(message,uid){
		if(!message || typeof(message)!='string'){
			console.log('发送的消息不合法！只能发送字符串格式消息！');
			return;
		}
		var app=getApp();
		message=app.util.base64.encode(message);
		var data={
			'message':message,
			'to_uid':uid,
		};
		//console.log('准备发送消息：');console.log(data);
		_message(data,'say');
	};
	jinyun_socket.send2room=function(message,room_id){
		if(!message || typeof(message)!='string'){
			console.log('发送的消息不合法！只能发送字符串格式消息！');
			return;
		}
		var app=getApp();
		message=app.util.base64.encode(message);
		var data={
			'message':message,
			'to_room':room_id,
		};
		_message(data,'say');
	};
	jinyun_socket.join=function(room_id){
		
		var data={
			'room_id':room_id,
		};
		_message(data,'join');
	};
	jinyun_socket.leave=function(room_id){
		
		var data={
			'room_id':room_id,
		};
		_message(data,'leave');
	};
	jinyun_socket.connect=function(callback,config,time){
		if(_ws){
			return;
		}
		if(!config){
			var app=getApp();
			app.util.get('core/tools.socket/',function(res){
				//console.log('socket_res');console.log(res);
				jinyun_socket.connect(callback,res.config,time);
			});
			return;
		}
		//console.log('当前用户ID：'+config.uid);
		if(!config.token || !config.nonce || !config.timestamp || !config.sign || !config.uid){
			console.log('参数错误，无法发起socket连接！');
			return;
		}
		_ws = wx.connectSocket({'url':_socket_url});
		//console.log('_ws');console.log(_ws);
		_ws.onOpen(function(){
			_message(config,'login');
			if(typeof(callback)=='function'){
				callback();
			}
		});
		_ws.onMessage(function(e){
			//console.log('收到消息！');console.log(e);
			var data = JSON.parse(e.data);
			var app=getApp();
			switch(data['type']){
				case 'ping':
					//心跳
					//console.log('心跳');
					_message('','pong');
					return;
				case 'error':
					//报错
					console.log('收到来自服务器的错误提示：');console.log(data.data);
					return;
				case 'say':
					//接收消息
					if(!jinyun_socket.onmessage || typeof(jinyun_socket.onmessage)!='function'){
						return;
					}
					var message={
						'message':app.util.base64.decode(data.data.message),
						'from_uid':data.data.from_uid,
						'from_room':data.data.from_room,
					};
					jinyun_socket.onmessage(message);
					return;
				case 'login':
					//登陆消息
					if(!jinyun_socket.onlogin || typeof(jinyun_socket.onlogin)!='function'){
						return;
					}
					var message={
						'uid':data.data.uid,
					};
					jinyun_socket.onlogin(message);
					return;
				case 'logout':
					//退出消息
					if(!jinyun_socket.onlogout || typeof(jinyun_socket.onlogout)!='function'){
						return;
					}
					var message={
						'uid':data.data.uid,
					};
					jinyun_socket.onlogout(message);
					return;
				case 'join':
					//进入房间消息
					if(!jinyun_socket.onjoin || typeof(jinyun_socket.onjoin)!='function'){
						return;
					}
					var message={
						'uid':data.data.uid,
						'room':data.data.room,
					};
					jinyun_socket.onjoin(message);
					return;
				case 'leave':
					//退出房间消息
					if(!jinyun_socket.onleave || typeof(jinyun_socket.onleave)!='function'){
						return;
					}
					var message={
						'uid':data.data.uid,
						'room':data.data.room,
					};
					jinyun_socket.onleave(message);
					return;
				case 'system':
					//来自系统的消息
					if(!jinyun_socket.onsystem || typeof(jinyun_socket.onsystem)!='function'){
						return;
					}
					var message={
						'message':data.data.message,
						'to_uid':data.data.to_uid,
						'to_room':data.data.to_room,
					};
					jinyun_socket.onsystem(message);
					return;
			}
		});
		_ws.onClose(function() {
			console.log("连接关闭!");
			/*time=time || 1000;
			setTimeout(function(){
				if(time<60000){
					time=time+time;
				}
				jinyun_socket.connect(callback,false,time);
			},time)*/
			
		});
		_ws.onError(function(e) {
			console.log("出现错误");console.log(e);
		});
		return true;
	}
	module.exports = jinyun_socket;
